/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.textdescriptor;

import java.util.HashMap;
import java.util.logging.Logger;

/**
 * This class stores the information descriptor definitions. 
 * 
 * @author Carter
 */
public abstract class A_TextDescriptorHandler {

	/** Logger Name */
	public static final String LOGGER_NAME = "com.acarter.scenemonitor.textdescriptor.A_TextDescriptorHandler";

	/** Logger */
	private static final Logger LOGGER = Logger.getLogger(A_TextDescriptorHandler.LOGGER_NAME);

	/** Set of descriptors for each tree object */
	protected HashMap<Class<?>, A_TextDescriptor> descriptors = new HashMap<Class<?>, A_TextDescriptor>();
	
	/**
	 * Constructor.
	 */
	public A_TextDescriptorHandler() {

		registerDescriptor(null, new DefaultTextDescriptor());
			
		initDescriptors();
	}
	
	/**
	 * Checks if the default library of descriptors is in the classpath.
	 * 
	 * @return True if it is found, false otherwise.
	 */
	public static boolean descriptorsExist() {

		// Look for the descriptor libraries
		try {
			Class.forName("com.acarter.scenemonitor.textdescriptor.definitions.SpatialDescriptor");

			return true;
		} catch (ClassNotFoundException e) {
			LOGGER.info("Scene Monitor cannot load 'com.acarter.scenemonitor.textdescriptor.definitions.SpatialDescriptor', no descriptor library has been loaded.");
		}
		
		return false;
	}
	
	/**
	 * Subclasses must load their own set of default descriptors.
	 */
	protected abstract void initDescriptors();
	
	/**
	 * Returns the descriptor that is associated with the object. If an exact match is not defined, then 
	 * it will search up the inheritance of the object to find the next closest match.
	 * 
	 * @param object The object who's descriptor you want returned.
	 * @return The descriptor matching the object type.
	 */
	public A_TextDescriptor getDescriptor(Object object) {

		A_TextDescriptor descriptor = null;
		
		if(object != null) {

			Class<?> c = object.getClass();
			
			descriptor = descriptors.get(c);
		
			if(descriptor == null) {
			
				while((c = c.getSuperclass()) != null) {

					if((descriptor = descriptors.get(c)) != null)
						break;
				}
			}
		}
		
		if(descriptor == null)
			descriptor = descriptors.get(null);
		
		return descriptor;
	}
	
	/**
	 * Adds the descriptor/object type pair to the collection.
	 * 
	 * @param classType
	 * @param descriptor
	 */
	public void registerDescriptor(Class<?> classType, A_TextDescriptor descriptor) {
		
		descriptors.put(classType, descriptor);
	}
}
